# coding=utf-8
# 代码文件：chapter12/ch12.3.2.py

import time
import tkinter as tk
import tkinter.ttk as ttk
from random import random
from tkinter import messagebox

window = tk.Tk()

window.title('进度条')
#  设定窗口的大小(宽x高)
window.geometry('320x130')

''' 定义onClick1函数 '''


def onClick1():
    # 启动进度条pb1动画，进度条滑块开始运动起来
    pb1.start()
    for i in range(5):
        window.update_idletasks()  # 任务处理空闲时更新界面
        pb1['value'] += 20  # 累加更新进度条数值

        time.sleep(1)

    # 任务完成调用stop()函数
    pb1.stop()
    messagebox.showinfo('信息', '任务1完成。')


def onClick2():
    # 进度条动画开始
    pb2.start()

    # 任务完成调用stop()函数


# 创建按钮对象
button1 = tk.Button(window, text='确定进度栏', command=onClick1)
button2 = tk.Button(window, text='不确定进度栏', command=onClick2)

# 创建水平进度条pb1对象
pb1 = tk.ttk.Progressbar(window,
                         orient=tk.HORIZONTAL,
                         mode='determinate',
                         length=100)
# 创建水平进度条pb2对象
pb2 = ttk.Progressbar(window,
                      orient=tk.HORIZONTAL,
                      length=10,
                      mode='indeterminate')

button1.pack(fill=tk.BOTH)
pb1.pack(fill=tk.BOTH)
button2.pack(fill=tk.BOTH)
pb2.pack(fill=tk.BOTH)

# 将窗口对象加入到主事件循环
window.mainloop()
